﻿using OpenAuth.Repository.Domain;
using System;
using System.Collections.Generic;
using System.Text;
using Infrastructure;
using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.Repository.Interface;


namespace OpenAuth.App
{
    public class RefundOrderApp : BaseApp<RefundOrderEntity>
    {
        private RevelanceManagerApp _revelanceApp;
        private IAuth _auth;
        public RefundOrderApp(IUnitWork unitWork, IRepository<RefundOrderEntity> repository,
            RevelanceManagerApp app, IAuth auth) : base(unitWork, repository)
        {
            _revelanceApp = app;
            _auth = auth;
        }
        public void Add(RefundOrderEntity pay)
        {
            Repository.Add(pay);
        }
        /// <summary>
        /// 通过退款状态获取本地库中的退款记录
        /// </summary>
        /// <param name="tkzt"></param>
        /// <returns></returns>
        public List<RefundOrderEntity> GetListBytkzt(int tkzt)
        {
            var query = Repository.Find(x => x.tkzt == tkzt && x.tkopenid!="0"&& x.time <= DateTime.Now.AddMinutes(-1));
            var items = query.MapToList<RefundOrderEntity>();
            return items;
        }
        public void Update(RefundOrderEntity obj)
        {
            var user = _auth.GetCurrentUser().User;
            UnitWork.Update<RefundOrderEntity>(u => u.Id == obj.Id, u => new RefundOrderEntity
            {
                OrderNumber = obj.OrderNumber,
                bxh = obj.bxh,
                hzxm = obj.hzxm,
                realtkje = obj.realtkje,
                tkje = obj.tkje,
                jhtkje = obj.jhtkje,
                tkzt = obj.tkzt,
                time = obj.time,
                tkPhone = obj.tkPhone,
                tkrealName = obj.tkrealName,
                patid = obj.patid,
                hisdh = obj.hisdh,
                sjh = obj.sjh,
                yjjxh = obj.yjjxh,
                tkNumber = obj.tkNumber
                //UpdateTime = DateTime.Now,
                //UpdateUserId = user.Id,
                //UpdateUserName = user.Name
                //todo:补充或调整自己需要的字段
            });

        }
        /// <summary>
        /// 添加加建行退款金额
        /// </summary>
        /// <param name="ordernum">订单号</param>
        /// <param name="realtkje">真实退款金额</param>
        /// <returns></returns>
        public void Addjhtkje(string ordernum, double jhtkje)
        {
            var entity = Repository.FindSingle(t => t.OrderNumber == ordernum);
            entity.jhtkje = jhtkje;
            Repository.Update(entity);
        }
        /// <summary>
        /// 通过ordernumber获取单条退款信息
        /// </summary>
        /// <param name="ordernum">订单号</param>
        /// <returns></returns>
        public RefundOrderEntity GetByOrderNum(string ordernum)
        {
            var entity = Repository.FindSingle(t => t.OrderNumber == ordernum);
            return entity;
        }
        /// <summary>
        /// 根据patid获取一个月内的退款List
        /// </summary>
        /// <param name="patid">患者id</param>
        /// <param name="stime">开始时间</param>
        /// <param name="etime">结束时间</param>
        /// <returns></returns>
        public List<RefundOrderEntity> GetOrderByPatid(string patid, DateTime stime, DateTime etime)
        {
            var query = Repository.Find(x => x.patid == patid && x.time <= etime && x.time >= stime);
            var items = query.MapToList<RefundOrderEntity>();
            return items;

        }
        public void Updateorder(RefundOrderEntity entity)
        {
            Repository.Update(entity);
        }
        /// <summary>
        /// 修改退款状态
        /// </summary>
        /// <param name="ordernum">订单号</param>
        /// <param name="code">1微信支付成功，2卫宁下单成功</param>
        /// <returns></returns>
        //public void Updateorder(string ordernum, int code)
        //{
        //    var entity = Repository.FindSingle(t => t.OrderNumber == ordernum);
        //    entity.tkzt = code;
        //    Repository.Update(entity);
        //}
        //public void Updateorder(string ordernum, int code, double realtkje)
        //{
        //    var entity = Repository.FindSingle(t => t.OrderNumber == ordernum);
        //    entity.tkzt = code;
        //    entity.realtkje = realtkje;
        //    Repository.Update(entity);
        //}
        /// <summary>
        /// 添加加卫宁退款金额
        /// </summary>
        /// <param name="ordernum">订单号</param>
        /// <param name="realtkje">真实退款金额</param>
        /// <returns></returns>
        public void AddRealtkje(string ordernum, double realtkje)
        {
            var entity = Repository.FindSingle(t => t.OrderNumber == ordernum);
            entity.realtkje = realtkje;
            Repository.Update(entity);
            Repository.Save();
        }
        /// <summary>
        /// 添加收据号His单号
        /// </summary>
        /// <param name="ordernum"></param>
        /// <param name="sjh"></param>
        /// <param name="hisdh"></param>
        /// <param name="yjjxh"></param>
        /// <returns></returns>
        public void Updateorder(string ordernum, string sjh, string hisdh, string yjjxh)
        {
            var entity = Repository.FindSingle(t => t.OrderNumber == ordernum);
            if (entity != null)
            {
                entity.hisdh = hisdh;
                entity.sjh = sjh;
                entity.yjjxh = yjjxh;
                Repository.Update(entity);
            }

        }
    }
}
